﻿using System;
using System.Collections.Generic;
using System.Text;

namespace KnifeZ.Unity.Utils
{
    public class RandomUtil
    {

        ///<summary>
        ///生成随机字符串 
        ///</summary>
        ///<param name="length">目标字符串的长度</param>
        ///<param name="useNumber">是否包含数字，1=包含，默认为包含</param>
        ///<param name="useLowerCase">是否包含小写字母，1=包含，默认为包含</param>
        ///<param name="useUpperCase">是否包含大写字母，1=包含，默认为包含</param>
        ///<param name="useSpecialCase">是否包含特殊字符，1=包含，默认为不包含</param>
        ///<param name="custom">要包含的自定义字符，直接输入要包含的字符列表</param>
        ///<returns>指定长度的随机字符串</returns>
        public static string GetRandomString(int length, bool useNumber = true, bool useLowerCase = true, bool useUpperCase = true, bool useSpecialCase = false, string custom = "")
        {
            byte[] b = new byte[4];
            new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
            Random r = new Random(BitConverter.ToInt32(b, 0));
            string s = null, str = custom;
            if (useNumber == true) { str += "0123456789"; }
            if (useLowerCase == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
            if (useUpperCase == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
            if (useSpecialCase == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
            for (int i = 0; i < length; i++)
            {
                s += str.Substring(r.Next(0, str.Length - 1), 1);
            }
            return s;
        }

        /// <summary>
        /// 获取指定范围的随机日期
        /// </summary>
        /// <param name="day">减去的最大天数</param>
        /// <returns>间隔日期之间的 随机日期</returns>
        public static DateTime GetRandomTime(int day, int x = 8, int y = 18)
        {
            Random random = new Random();
            var h = random.Next(x, y);
            var m = random.Next(0, 60);
            var s = random.Next(0, 60);
            //先把时间转换成年月日的字符串格式 在转换成时间（把时间格式转换成日期格式）
            DateTime dateTime = DateTime.Now;
            var add = dateTime.AddDays(-random.Next(0, day));
            //减去的天数加上随机生成的时间得到我们自己需要的时间
            var newDateTime = Convert.ToDateTime(add.ToString("yyyy-MM-dd 00:00:00")) + TimeSpan.Parse(h + ":" + m + ":" + s);
            //返回需要的时间
            return newDateTime;
        }


    }
}
